package TestDemo.example.service.impl;

import TestDemo.example.mapper.EmpExprMapper;
import TestDemo.example.mapper.EmpMapper;
import TestDemo.example.pojo.*;
import TestDemo.example.service.EmpService;
import TestDemo.example.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    private static final Logger log = LoggerFactory.getLogger(EmpServiceImpl.class);
    @Autowired
    EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    //自己实现分页功能
//    @Override
//    public PageResult page(Integer page, Integer pageSize) {
//        PageResult pageResult = new PageResult();
//        pageResult.setTotal(empMapper.empTotal());
//        Integer start = (page - 1) * pageSize;
//        pageResult.setRows(empMapper.findEmpRows(start, pageSize));
//        return pageResult;
//    }
    //使用分页插件PagingHelper
    @Override
    public PageResult page(Integer page, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行查询
        ArrayList<Emp> emplist = empMapper.findEmplist();
        log.info(empMapper.findEmplist().toString());
        Page< Emp> pagelist = (Page< Emp>) emplist;
        //解析查询结果并封装成PageResult
        return new PageResult(pagelist.getTotal(),pagelist.getResult());
    }

    @Override
    public PageResult pageEmp(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        Page<Emp> empPage = (Page<Emp>) empMapper.findEmp(empQueryParam);
        log.info(empMapper.findEmp(empQueryParam).toString());
        return new PageResult(empPage.getTotal(), empPage.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
        log.info("***************新增员工id{}*****************",emp.getId());
        ArrayList<EmpExpr> empExprList=emp.getExprList();
        if (empExprList.size() != 0) {
            empExprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(empExprList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmpById(ArrayList<Integer> idArrayList) {
        empMapper.deleteDeptById(idArrayList);
        log.info("***************删除员工id{}",idArrayList);
        empExprMapper.deleteEmpExprById(idArrayList);
        log.info("***************删除员工经历");
    }

    @Override
    public Emp getById(Integer id) {
       return empMapper.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Emp emp) {
        empMapper.updateEmp(emp);
        log.info("***************更新员工id{}",emp.getId());
        ArrayList<Integer> list=new ArrayList<>();
        list.add(emp.getId());
        ArrayList<Integer> idArrayList = new ArrayList<>(list);

        empExprMapper.deleteEmpExprById(idArrayList);
        log.info("***************删除员工经历");

        if (!CollectionUtils.isEmpty(emp.getExprList()) ) {
            emp.getExprList().forEach(empExpr -> {
            empExpr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(emp.getExprList());
        }
        log.info("***************插入员工经历");
    }

    @Override
    public JobOption getEmpJobData() {
       List<Map<String, Integer>> mapList=empMapper.getEmpJobData();
        List<String> jobList=new ArrayList<>() ;
        List<Integer> dataList=new ArrayList<>() ;
        for (Map map : mapList) {
            jobList.add((String) map.get("pos"));
            Integer num = ((Number) map.get("num")).intValue();
            dataList.add(num);
        }
        log.info("员工职位名称{}" ,jobList);
        log.info("员工职位数据{}" ,dataList);
        return new JobOption(jobList,dataList);
    }

    @Override
    public List<Map<String, Object>> getEmpGenderData() {
        return empMapper.getEmpGenderData();
    }

    @Override
    public LoginInfo getLoginInfo(Emp emp) {
        LoginInfo loginInfo = empMapper.getLoginInfo(emp);
        if (loginInfo != null) {
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("id", loginInfo.getId());
            claims.put("username", loginInfo.getUsername());
            String jwt= JwtUtils.generateToken(claims, loginInfo.getUsername());
            loginInfo.setToken(jwt);
        }
        log.info("登录信息{}", loginInfo);
        return loginInfo;
    }
}
